package store

import (
	"guard/internal/app/user/models"
	"guard/internal/db"
	"guard/internal/tool"
)

var userSql = "select * from `user` where username = ?"

func (s *userStoreImpl) SelUser(username string) (id int, userName, password, nickname, picture, userScopeTag string, enable bool, err error) {
	var user models.User
	err = s.db.Raw(userSql, username).Scan(&user).Error
	return user.Id, user.Username, user.Password, user.Nickname, user.Picture, user.UserScopeTag, user.Enable, err
}

/**
管理user用
*/

func (s *userStoreImpl) SelUserPage(current, size int) *tool.Paginator {
	return db.SelPage(&models.User{}, current, size, s.db)
}

func (s *userStoreImpl) SelUserQuery(id int, username string) (mu *models.User, err error) {
	err = s.db.Raw("select * from user where id = ? or username = ?", id, username).Find(&mu).Error
	return
}

func (s *userStoreImpl) SaveOrUpdateUser(user models.User) error {
	return s.db.Save(&user).Error
}

func (s *userStoreImpl) DeleteUser(username string) error {
	return s.db.Exec("delete from user where username = ?", username).Error
}
